const Joi = require('joi')

// 定义用户注册参数：手机号、验证码、账号、昵称、登录密码
const phone = Joi.string()
  .pattern(/^1[3-9]\d{9}$/)
  .required()
const code = Joi.string()
  .pattern(/^\d{6}$/)
  .required()
const account = Joi.string().alphanum().max(64).min(1).required()
const password = Joi.string().min(6).max(16).alphanum().required()
const nickname = Joi.string().min(1).max(64).required()

// 定义用户登录验证规则
const login_phone = Joi.string().pattern(/^1[3-9]\d{9}$/)
const login_code = Joi.string().pattern(/^\d{6}$/)
const login_account = Joi.string().max(64).min(1)
const login_password = Joi.string().min(6).max(16).alphanum()

// 用户头像验证规则
const avatar = Joi.string().dataUri().required()
const type = Joi.string()
  .pattern(/\.jpg|\.png|\.jpeg|\.gif/)
  .required()

// 用户更新信息验证规则
const sex = Joi.string().valid('男', '女').required()
const email = Joi.string().email().required()
const birthday = Joi.string()
  .pattern(/^\d{4}-\d{2}-\d{2}$/)
  .required()

// 用户新增收货地址
const name = Joi.string().max(64).required()
const province = Joi.string().required()
const city = Joi.string().required()
const country = Joi.string().required()
const address = Joi.string().required()
const status = Joi.number().valid(0, 1).required()
const areacode = Joi.string()
  .pattern(/^\d{6}$/)
  .required()

// 用户收藏和取消收藏验证规则
const id = Joi.number().min(1).required()
const count = Joi.number().min(1).required()
// 用户获取商品传递的page参数
const page = Joi.number().min(1)

const goods_ids = Joi.string()
  .pattern(/^\[{"id":\d+,"count":\d+}(,{"id":\d+,"count":\d+})*\]$/)
  .required()
const address_id = Joi.number().min(1).required()

// 获取订单详情订单编号
const orderId = Joi.string().alphanum().length(20).required()

//#region
// 用户注册验证规则
exports.user_register = {
  body: {
    phone,
    code,
    account,
    password,
    nickname
  }
}

// 用户登录验证规则
exports.user_login_schema = {
  body: {
    phone: login_phone,
    code: login_code,
    account: login_account,
    password: login_password
  }
}

// 用户忘记密码验证规则
exports.user_forget_schema = {
  body: {
    phone,
    code,
    password,
    newPassword: Joi.valid(Joi.ref('password')).concat(password)
  }
}

// 导出用户更新头像验证规则
exports.user_update_avatar = {
  body: {
    avatar,
    type
  }
}

// 用户修改密码
exports.user_update_password_schema = {
  body: {
    password,
    newPassword: Joi.not(Joi.ref('password')).concat(password)
  }
}

// 用户更新用户信息
exports.update_userinfo_schema = {
  body: {
    account,
    phone,
    nickname,
    email,
    sex,
    birthday
  }
}
//#endregion

// 用户新增收货地址
exports.add_address_schema = {
  body: {
    name,
    phone,
    province,
    city,
    country,
    address,
    status,
    areacode
  }
}

// 用户编辑收货地址
exports.update_address_schema = {
  body: {
    name,
    phone,
    province,
    city,
    country,
    address,
    status,
    areacode
  }
}

// 获取推荐和最新商品传递的page验证规则
exports.get_new_goods_schema = {
  query: {
    page
  }
}

// 获取其他商品数据page验证规则
exports.get_another_goods_schema = {
  query: {
    page: Joi.number().min(1).required(),
    limit: Joi.number().min(1).required()
  }
}

// 收藏和取消收藏
exports.add_cancel_like_schema = {
  body: {
    id
  }
}

// 获取商品收藏信息
exports.get_goods_collection_schema = {
  query: {
    id
  }
}

// 获取所有的收藏商品
exports.get_all_collection_schema = {
  query: {
    page: Joi.number().min(1).required(),
    limit: Joi.number().min(1).required()
  }
}

// 添加到购物车
exports.add_cart_schema = {
  body: {
    count,
    id
  }
}

// 根据id删除购物车数据
exports.del_shopCart_schema = {
  body: {
    ids: Joi.string()
      .pattern(/^\d+,?(\d+,?)*$/)
      .required()
  }
}

// 修改购物车商品数量
exports.edit_cart_goods_count = {
  body: {
    count,
    id,
    goods_id: id
  }
}

// 根据分类id获取所有商品
exports.get_all_goods_by_cate_id = {
  query: {
    id,
    page: Joi.number().min(1).required(),
    limit: Joi.number().min(1).required()
  }
}

// 获取二级分类验证规则
exports.get_one_level_schema = {
  query: {
    id
  }
}

// 搜索参数验证规则
exports.search_keyword_schema = {
  query: {
    keyword: Joi.string().min(1).max(64).required()
  }
}

// 删除一条搜索历史的id验证
exports.del_search_history_schema = {
  body: {
    id: Joi.number().min(1).required()
  }
}

// 提交订单参数验证规则
exports.submit_order_schema = {
  body: {
    goods_ids,
    address_id
  }
}

// 从购物车提交订单参数验证规则
exports.submit_cart_order_schema = {
  body: {
    goods_ids,
    address_id,
    ids: Joi.string()
      .pattern(/^\d+,?(\d+,?)*$/)
      .required()
  }
}

// 根据订单编号获取订单详情数据
exports.get_order_id_schema = {
  query: {
    orderId
  }
}

// 根据状态获取订单数据
exports.get_order_status_schema = {
  query: {
    status: Joi.number().valid(0, 1, 2, 3).required(),
    page: Joi.number().min(1).required(),
    limit: Joi.number().min(1).required()
  }
}

// 获取全部订单数据
exports.get_all_order_schema = {
  query: {
    page: Joi.number().min(1).required(),
    limit: Joi.number().min(1).required()
  }
}

// 删除订单参数验证规则
exports.del_order_schema = {
  body: {
    orderId
  }
}

// 支付接口的眼熟验证
exports.payment_schema = {
  body: {
    orderId,
    totalPrice: Joi.number().min(0).required()
  }
}

// 查询订单支付结果参数验证
exports.query_pay_result_schema = {
  query: {
    orderId
  }
}
